/**
 * 
 */
package org.nightrunner.d3;

import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.nightrunner.d3.entity.Property;
import org.nightrunner.d3.log.SysLog;
import org.nightrunner.d3.service.ICategoryService;
import org.nightrunner.d3.service.IPropertyService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * @author haiyang.chen
 * @date 2012-1-12
 * 
 */
public class CacheTester extends BaseSpringTester {

	protected static Logger LOGGER = SysLog.TEST;

	@Autowired
	private IPropertyService propertyService;

	public IPropertyService getPropertyService() {
		return propertyService;
	}

	public void setPropertyService(IPropertyService propertyService) {
		this.propertyService = propertyService;
	}

	@Autowired
	private ICategoryService categoryService;

	public ICategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(ICategoryService categoryService) {
		this.categoryService = categoryService;
	}

	@Test
	public void testCacheSpeed() {

		// hibernate热身
		for (int i = 0; i < 100; i++) {
			LOGGER.info(categoryService.getAllCategory().size());
		}

		long start = 0;
		long end = 0;
		long size = 0;
		long time = 0;
		long totalTime = 0;
		int count = 0;
		long firstTime = 0;
		List<Property> propertys = null;
		for (int i = 0; i < 100; i++, count++) {
			start = System.currentTimeMillis();
			propertys = propertyService.getAllProperty();
			size = propertys.size();
			for (Property prop : propertys) {
				prop.getPropertyAbbreviationName();
				prop.getPropertyName();
				prop.getPropertyDescription();
			}
			end = System.currentTimeMillis();
			time = end - start;
			LOGGER.info(end - start);
			totalTime += time;
			if (i == 0) {
				firstTime = time;
			}

		}
		LOGGER.info("数据量:" + size + ",测试次数:" + count + ",第一次所用时间:" + firstTime
				+ ",平均时间:" + (totalTime / count));
	}

	@Test
	public void testAdd() {
		Property property = null;
		for (int i = 0; i < 5000; i++) {
			property = new Property();
			property.setPropertyName("属性" + i);
			property.setPropertyAbbreviationName("1");
			property.setPropertyDescription("描述" + i);
			property.setPropertyKeywords("关键字 ");
			propertyService.updateProperty(property);
			if (i % 1000 == 0) {
				System.out.println(i);
			}
		}
	}
}
